package com.oa.user.DaoImpl;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.oa.entity.TblUser;
import com.oa.user.Dao.UserDao;

public class UserDaoImpl implements UserDao {
	
	private HibernateTemplate hibernateTemplate;
	
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
	/*
	 * 信息加载
	 * @see com.oa.usre.Dao.UserDao#load(java.lang.Integer)
	 */
	@Override
	public TblUser load(Integer Id) {
		// TODO Auto-generated method stub
		TblUser tu = new TblUser();
		try{
			tu = (TblUser) this.hibernateTemplate.load(TblUser.class, Id);
		}catch(Exception e){
			e.printStackTrace();
		}
		return tu;
	}
	/*
	 * 信息添加
	 * @see com.oa.usre.Dao.UserDao#add(com.oa.entity.TblUser)
	 */
	@Override
	public boolean add(TblUser user) {
		// TODO Auto-generated method stub
		boolean flag = false;
		try{
			this.hibernateTemplate.save(user);
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return flag;
	}
	/*
	 * 信息删除
	 * @see com.oa.usre.Dao.UserDao#delete(com.oa.entity.TblUser)
	 */
	@Override
	public boolean delete(TblUser user) {
		boolean flag = false;
		try {
			this.hibernateTemplate.delete(user);
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	/*
	 * 信息删除
	 * @see com.oa.usre.Dao.UserDao#update(com.oa.entity.TblUser)
	 */
	@Override
	public boolean update(TblUser user) {
		// TODO Auto-generated method stub
		boolean flag = false;
		try{
			this.hibernateTemplate.update(user);
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return flag;
	}
	/*
	 * 信息查询
	 * @see com.oa.usre.Dao.UserDao#searchTargetDatas(java.lang.String, java.util.Map)
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public List searchTargetDatas(String hql, Map params) {
		// TODO Auto-generated method stub
		Query query = null;
		List list = null;
		try {
			if (params != null&&params.size() > 0) {
				query = this.hibernateTemplate.getSessionFactory().openSession().createQuery(hql);
				Object[] keyArrs = params.keySet().toArray();
				for (int i = 0; i < keyArrs.length; i++) {
					query.setParameter(keyArrs[i].toString(),params.get(keyArrs[i]));
				}
				list = query.list();
			} else {
				list = this.hibernateTemplate.find(hql);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}
